/**
 * 2014年12月4日 下午4:31:37
 */
package io.renren.common.utils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author anxlng
 *
 */
public class CodeGenerator {

	public static String generateSMSCode() {
		return generateCodeByNum(4);
	}

	public static String generateCodeByChars(int size) {
		final String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
		final int length = chars.length();
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0, n = -1; i < size;) {
			int intChar = random.nextInt(length);
			if (intChar != n) {
				sb.append(chars.charAt(intChar));
				n = intChar;
				i++;
			}
		}
		return sb.toString();
	}

	public static String generateCodeByNum(int size) {
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0, n = -1; i < size;) {
			int intChar = random.nextInt(10);
			if (intChar != n) {
				sb.append(intChar);
				n = intChar;
				i++;
			}
		}
		return sb.toString();
	}

	/**
	 * 返回20位带有时间戳和随机数生成的编码
	 * 
	 * @return
	 */
	public static String generateByTimestamp() {
		return DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS") + generateCodeByChars(3);
	}

	public static String generateUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
}
